package com.sl.au.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.InventoryDull;
import com.sl.au.entity.QuoteDevice;
import com.sl.au.service.InventoryDullService;

@RestController
@RequestMapping(value="/ui/inventoryDull")
public class InventoryDullController extends BaseController{
	@Autowired
	InventoryDullService inventoryDullService;
	
	 @RequestMapping(method = RequestMethod.GET)
		public ModelAndView index(){
			ModelAndView modelAndView = createLayoutView("inventoryDull/inventoryDullindex");
		return modelAndView;
		}
	 //得到呆滞库页面
		@RequestMapping(value="/getpage",method = RequestMethod.GET)
		public Map<String, Object> getPage(HttpServletRequest request){
			Map<String, Object> map=new HashMap<String, Object>();
			int start = Integer.parseInt(request.getParameter("start"));
			int length = Integer.parseInt(request.getParameter("length"));
			Long count=null;
			String key=request.getParameter("key");
			List<InventoryDull> inventoryDulls=inventoryDullService.getInventoryDullBykey(start, length, key, null, null);
				if(key==null){
					count= inventoryDullService.getRepository().count();
				}
				else if(key!=null){
					count=inventoryDullService.getRepository().getCountBy(key);
			}
			map.put("data", inventoryDulls);
			map.put("draw", request.getParameter("draw"));
			map.put("recordsFiltered", count);
			map.put("recordsTotal", count);
			return map;
		}
		 //价格单可替换设备
		@RequestMapping(value="/getavailablePage",method = RequestMethod.GET)
		public Map<String, Object> getavaliable(HttpServletRequest request){
			Map<String, Object> map=new HashMap<String, Object>();
			int start = Integer.parseInt(request.getParameter("start"));
			int length = Integer.parseInt(request.getParameter("length"));
			Long count=null;
			String key=request.getParameter("key");
			//根据可用数量得价格单主机降库可替换设备
			List<InventoryDull>	inventoryDulls = inventoryDullService.getInventoryDull(start, length, key, null, null);
				if(key==null){
					count= inventoryDullService.getRepository().findNoReplaceCount();
				}
				else if(key!=null){
				count=inventoryDullService.getRepository().findCount(key);
				}
			map.put("data", inventoryDulls);
			map.put("draw", request.getParameter("draw"));
			map.put("recordsFiltered", count);
			map.put("recordsTotal", count);
			return map;
		}
		
		//还原已替换设备
		@RequestMapping(value="/returnInventory",method=RequestMethod.POST)
		public Map<String, Object> saveDevice(HttpServletRequest request){
			Map<String, Object> map = new HashMap<String, Object>();
			String inventoryDullId=request.getParameter("inventoryDullId");
			int number=Integer.parseInt(request.getParameter("number"));
			String quoteDeviceId = request.getParameter("quoteDeviceId");
			InventoryDull inventoryDull = inventoryDullService.getRepository().findOne(inventoryDullId);
			int usableNum = inventoryDull.getUsableNum();
			inventoryDull.setUsableNum(usableNum+number);
			inventoryDullService.getRepository().save(inventoryDull);
			List<QuoteDevice> quoteDevices = inventoryDullService.getDeviceRepository().findLinkDevice(quoteDeviceId);
			inventoryDullService.getDeviceRepository().deleteInBatch(quoteDevices);
			return map;
		}
		
}
